<template>
  <el-container>
    <el-header>
    <!-- 头部导航 -->
      <el-row>
        <el-col :span="4">
          <div class="grid-content bg-purple">
            <img src="../assets/体育-篮球.png" alt="" />
          </div>
        </el-col>
        <el-col :span="16">
          <div class="grid-content bg-purple-light">电商后台管理</div>
        </el-col>
        <el-col :span="4">
        <!-- 退出登录 -->
          <div class="grid-content bg-purple">
            <el-dropdown  @command="outUsers">
              <span class="el-dropdown-link">
                {{ userinfo.username }}
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item icon="el-icon-error" 
                  >退出登录</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </el-col>
      </el-row>
    </el-header>
    <!-- 遍历侧边栏 -->
    <el-container class="select">
      <el-aside width="200px">
        <el-menu
          default-active="2"
          class="el-menu-vertical-demo"
          :unique-opened="true"
          :router="true"
        >
          <el-submenu
            :index="item.path"
            v-for="(item, index) in menudata"
            :key="index"
          >
            <template slot="title">
              <i class="el-icon-menu"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item-group>
              <el-menu-item
                :index="val.path"
                v-for="(val, key) in item.children"
                :key="key"
              >
                <i class="el-icon-document"></i>
                {{ val.authName }}
              </el-menu-item>
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 中间内容区 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      //存放用户数据
      userinfo: {},
      //遍历侧边栏数组
      menudata: [],
    };
  },

  async mounted() {
    // const userinfo = localStorage.getItem("userinfo");
    //获取本地的数据来设置用户登录的生名周期
    //获取用户数据
    this.userinfo = JSON.parse(localStorage.getItem("userinfo"));

    //获取侧边栏数据
    var { data } = await this.$axios.get("/menus");
    // console.log(data);
    if (data.meta.status == 200) {
      this.menudata = data.data;
    } else {
      this.$message({
        type: "error",
        message: data.meta.msg,
      });
    }
  },
  methods: {
    
    //退出登录
    outUsers() {
      this.$message({
        type: "success",
        message: "退出登录成功！",
      });
      //清除本地储存的token值
      localStorage.removeItem("userinfo");
      //跳转页面，跳转到login
      this.$router.push({ path: "/login" });
    },
  },
};
</script>

<style>
.el-header,
.el-footer {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}
body {
  padding: 0;
  margin: 0;
}
img {
  width: 45px;
}
.el-form-item__content{
width: 23vw;
}
</style>